package net.aib.moneytracker;

import net.aib.moneytracker.db.DBHelper;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class OldBuys extends ListActivity {
	private static String TAG = "OldBuys";
	
	private MoneyTrackerController mMTController;
	private DBHelper mDBHelper;
	private Cursor mCursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.buylist);

        mMTController = new MoneyTrackerController(this);
        mCursor = mMTController.getBuys(); 
        startManagingCursor(mCursor);

        ListAdapter adapter = new SimpleCursorAdapter(
                this,
                R.layout.buyslistrow,
                mCursor, 
                new String[] {mDBHelper.BUY_KEY_AMOUNT, 
                		mDBHelper.BUY_KEY_DATE, 
                		"catname", 
                		"copname"},  
                new int[] {R.id.amount, R.id.date, R.id.buycategory, R.id.buycompany});

        setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.d(TAG, "id = " + id);
		editBuy(id);
	}
	
	private void editBuy(long id){
		Cursor buy = mMTController.getBuy(id);
		final Double curAmount = buy.getDouble(buy.getColumnIndex(DBHelper.BUY_KEY_AMOUNT));
		final long bId = id; 
		
		AlertDialog.Builder builder;
    	AlertDialog buyDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.editbuy, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eBuy = (EditText) layout.findViewById(R.id.editBuy);
    	    	
        eBuy.setText(Double.toString(curAmount));
    	
    	builder.setView(layout)
    		   .setTitle(R.string.edit_buy)
    		   .setPositiveButton(R.string.edit, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(eBuy.getText().length() > 0){
						Log.d(TAG,"Edit buy amount = " + eBuy.getText());
						mMTController.editBuy(bId, Double.parseDouble(eBuy.getText().toString()));
						mMTController.addToAccount(curAmount - Double.parseDouble(eBuy.getText().toString()));
						mCursor.requery();
						setResult(RESULT_OK);
						dialog.dismiss();
					}
					else{
						Toast.makeText(getApplicationContext(), "Vantar texta", Toast.LENGTH_LONG).show();
					}
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	buyDialog = builder.create();
    	buyDialog.show();
	}
}
